package com.esiag.isidis.fi.pdsteam.content;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JPanel;

import com.esiag.isidis.fi.pdsteam.model.Event;
import com.esiag.isidis.fi.pdsteam.model.EventInfo;
import com.esiag.isidis.fi.pdsteam.model.EventList;
import com.esiag.isidis.fi.pdsteam.reader.MarshalerXML;
import com.esiag.isidis.fi.pdsteam.model.SensorType;
import com.esiag.isidis.fi.pdsteam.views.Frame_Scenario;
import com.esiag.isidis.fi.pdsteam.views.LabledCombo;
import com.esiag.isidis.fi.pdsteam.views.LabledTextField;

public class EventInfoFinderfromUI implements EventInfoFinder, ActionListener{

	private static final String EVENTSTORE_XML = "./eventstore-jaxb.xml";

	private ArrayList<EventInfo> eventInfoStore =new ArrayList<EventInfo>();
	private EventList eventsStore=new EventList();
	
	private Frame_Scenario frame;
	private EventFinder eFinder;

	public EventList getEventsStore() {
		return eventsStore;
	}

	public EventInfoFinderfromUI(Frame_Scenario frame, EventFinder eFinder) {
		super();
		this.frame = frame;
		this.eFinder = eFinder;
	}

	public ArrayList<EventInfo> getEventsInfos() {
		
		return eventInfoStore;
	}

	public void actionPerformed(ActionEvent e) {
		ArrayList<Event> cumul=new ArrayList<Event>();
		JPanel container=frame.getLinesContainer();
		for (int i = 0; i < container.getComponentCount(); i++) {
			JPanel line=(JPanel)container.getComponent(i);
			LabledCombo cmbType =(LabledCombo)line.getComponent(0);
			LabledTextField txtCount=(LabledTextField) line.getComponent(1);
			LabledTextField txtWait=(LabledTextField) line.getComponent(2);
			
			EventInfo eInfo =new EventInfo(SensorType.valueOf(cmbType.getText())
											, Integer.parseInt(txtCount.getText())
											, Integer.parseInt(txtWait.getText()));
			eventInfoStore.add(eInfo);
			System.out.println(eInfo);
			
			ArrayList<Event> events= eFinder.getEventsByCriticity(eInfo);
			cumul.addAll(events);
			System.out.println( "size: " + cumul.size());
		}
		
		eventsStore.setEventsList(cumul);
		MarshalerXML marshaler = new MarshalerXML( );
		marshaler.marshal( EventList.class, eventsStore, EVENTSTORE_XML);
	}
}
